home *** CD-ROM | disk | FTP | other *** search
/ The Java 3D API Specification (2nd Edition) / The Java 3D API Specification (2nd Edition).iso / programs / examples / GeometryByReference / InterleavedTest.java < prev    next >
Text File  |  2000-04-28  |  11KB  |  363 lines

  1. /*
  2.  *    @(#)InterleavedTest.java 1.4 00/02/10 13:14:01
  3.  *
  4.  * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.*;
  33. import java.awt.event.*;
  34. import com.sun.j3d.utils.applet.MainFrame;
  35. import com.sun.j3d.utils.geometry.*;
  36. import com.sun.j3d.utils.universe.*;
  37. import javax.media.j3d.*;
  38. import javax.vecmath.*;
  39. import javax.swing.*;
  40. import javax.swing.event.*;
  41. import javax.swing.border.*;
  42. import com.sun.j3d.utils.behaviors.mouse.*;
  43.  
  44. public class InterleavedTest extends JApplet implements ActionListener {
  45.  
  46.     RenderingAttributes ra;
  47.     ColoringAttributes ca;
  48.     Material mat;               
  49.     Appearance app;               
  50.     JComboBox geomType;
  51.     JCheckBox transparency;
  52.     Shape3D shape;               
  53.     TransparencyAttributes transp;
  54.  
  55.     GeometryArray tetraRegular, tetraStrip, tetraIndexed, tetraIndexedStrip;
  56.     GeometryArray[] geoArrays = new GeometryArray[4];
  57.  
  58.    // Globally used colors
  59.    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  60.    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
  61.    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
  62.    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
  63.    Color3f[] colors = {white, red, green, blue};               
  64.  
  65.     private static final float sqrt3 = (float) Math.sqrt(3.0);
  66.     private static final float sqrt3_3 = sqrt3 / 3.0f;
  67.     private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
  68.  
  69.     private static final float ycenter = 0.5f * sqrt24_3;
  70.     private static final float zcenter = -sqrt3_3;
  71.  
  72.     private static final Point3f p1 = 
  73.     new Point3f(-1.0f, -ycenter, -zcenter);
  74.     private static final Point3f p2 = 
  75.     new Point3f(1.0f, -ycenter, -zcenter);
  76.     private static final Point3f p3 =
  77.     new Point3f(0.0f, -ycenter, -sqrt3 - zcenter);
  78.     private static final Point3f p4 =
  79.     new Point3f(0.0f, sqrt24_3 - ycenter, 0.0f);
  80.  
  81.     
  82.     private static final Color3f c1 = new Color3f(1.0f, 0.0f, 0.0f);
  83.     private static final Color3f c2 = new Color3f(0.0f, 1.0f, 0.0f);
  84.     private static final Color3f c3 = new Color3f(0.0f, 1.0f, 1.0f);
  85.     private static final Color3f c4 = new Color3f(1.0f, 1.0f, 0.0f);
  86.     
  87.  
  88.     private static final float[] interleaved = {
  89.     c1.x, c1.y, c1.z, // front face
  90.     p1.x, p1.y, p1.z, // front face
  91.     c2.x, c2.y, c2.z,
  92.     p2.x, p2.y, p2.z,
  93.     c4.x, c4.y, c4.z,
  94.     p4.x, p4.y, p4.z,
  95.     
  96.     c1.x, c1.y, c1.z,// left, back face
  97.     p1.x, p1.y, p1.z,// left, back face
  98.     c4.x, c4.y, c4.z,
  99.     p4.x, p4.y, p4.z,
  100.     c3.x, c3.y, c3.z,
  101.     p3.x, p3.y, p3.z,
  102.     
  103.     c2.x, c2.y, c2.z,// right, back face
  104.     p2.x, p2.y, p2.z,// right, back face
  105.     c3.x, c3.y, c3.z,
  106.     p3.x, p3.y, p3.z,
  107.     c4.x, c4.y, c4.z,
  108.     p4.x, p4.y, p4.z,
  109.     
  110.     c1.x, c1.y, c1.z,// bottom face
  111.     p1.x, p1.y, p1.z,// bottom face
  112.     c3.x, c3.y, c3.z,
  113.     p3.x, p3.y, p3.z,
  114.     c2.x, c2.y, c2.z,
  115.     p2.x, p2.y, p2.z,
  116.     };
  117.  
  118.     private static final float[] indexedInterleaved = {
  119.     c1.x,c1.y,c1.z,
  120.     p1.x,p1.y,p1.z,
  121.     c2.x,c2.y,c2.z,
  122.     p2.x,p2.y,p2.z,
  123.     c3.x,c3.y,c3.z,
  124.     p3.x,p3.y,p3.z,
  125.     c4.x,c4.y,c4.z,
  126.     p4.x,p4.y,p4.z,
  127.     };
  128.     
  129.  
  130.     private static final int[] indices = {0,1,3,0,3,2,1,2,3,0,2,1};
  131.     private int[] stripVertexCounts = {3,3,3,3};
  132.  
  133.  
  134.    BranchGroup createSceneGraph() {
  135.     BranchGroup objRoot = new BranchGroup();
  136.  
  137.     // Set up attributes to render lines
  138.         app = new Appearance();
  139.     
  140.     transp = new TransparencyAttributes();
  141.     transp.setTransparency(0.5f);
  142.     transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
  143.     transp.setTransparencyMode(TransparencyAttributes.NONE);
  144.     app.setTransparencyAttributes(transp);
  145.  
  146.     tetraRegular = createGeometry(1);
  147.     tetraStrip =createGeometry(2);
  148.     tetraIndexed = createGeometry(3);
  149.     tetraIndexedStrip = createGeometry(4);
  150.  
  151.     geoArrays[0] = tetraRegular;
  152.     geoArrays[1] = tetraStrip;
  153.     geoArrays[2] = tetraIndexed;
  154.     geoArrays[3] = tetraIndexedStrip;
  155.     
  156.     shape = new Shape3D(tetraRegular, app);
  157.     shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
  158.  
  159.     Transform3D t = new Transform3D();
  160.     // move the object upwards
  161.     t.set(new Vector3f(0.0f, 0.3f, 0.0f));
  162.  
  163.     // rotate the shape
  164.     Transform3D temp = new Transform3D();
  165.         temp.rotX(Math.PI/4.0d);
  166.     t.mul(temp);
  167.         temp.rotY(Math.PI/4.0d);
  168.         t.mul(temp);
  169.     
  170.     // Shrink the object 
  171.     t.setScale(0.6);
  172.  
  173.     TransformGroup trans = new TransformGroup(t);
  174.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  175.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  176.  
  177.     objRoot.addChild(trans);
  178.     trans.addChild(shape);
  179.  
  180.     BoundingSphere bounds =
  181.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  182.  
  183.     // Create the drag behavior node
  184.     MouseRotate behavior = new MouseRotate();
  185.     behavior.setTransformGroup(trans);
  186.     trans.addChild(behavior);
  187.     behavior.setSchedulingBounds(bounds);
  188.     
  189.     // Create the zoom behavior node
  190.     MouseZoom behavior2 = new MouseZoom();
  191.     behavior2.setTransformGroup(trans);
  192.     trans.addChild(behavior2);
  193.     behavior2.setSchedulingBounds(bounds);
  194.     
  195.     // Create the zoom behavior node
  196.     MouseTranslate behavior3 = new MouseTranslate();
  197.     behavior3.setTransformGroup(trans);
  198.     trans.addChild(behavior3);
  199.     behavior3.setSchedulingBounds(bounds);
  200.  
  201.  
  202.  
  203.     // Set up the global lights
  204.     Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  205.     Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
  206.     Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
  207.  
  208.     AmbientLight aLgt = new AmbientLight(alColor);
  209.     aLgt.setInfluencingBounds(bounds);
  210.     DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  211.     lgt1.setInfluencingBounds(bounds);
  212.     objRoot.addChild(aLgt);
  213.     objRoot.addChild(lgt1);
  214.     
  215.     // Let Java 3D perform optimizations on this scene graph.
  216.         objRoot.compile();
  217.  
  218.     return objRoot;
  219.     }
  220.  
  221.     JPanel createGeometryByReferencePanel() {
  222.     JPanel panel = new JPanel();
  223.     panel.setBorder(new TitledBorder("Geometry Type"));
  224.  
  225.     String values[] = {"Array", "Strip", "Indexed", "IndexedStrip"};
  226.     geomType = new JComboBox(values);
  227.     geomType.setLightWeightPopupEnabled(false);
  228.     geomType.addActionListener(this);
  229.     geomType.setSelectedIndex(0);
  230.     panel.add(new JLabel("Geometry Type"));     
  231.     panel.add(geomType);
  232.  
  233.     transparency = new JCheckBox("EnableTransparency", 
  234.                       false);
  235.     transparency.addActionListener(this);
  236.     panel.add(transparency);
  237.  
  238.     return panel;
  239.     }
  240.  
  241.  
  242.     public InterleavedTest() {
  243.     Container contentPane = getContentPane();
  244.     
  245.         Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  246.         contentPane.add("Center", c);
  247.  
  248.         BranchGroup scene = createSceneGraph();
  249.         // SimpleUniverse is a Convenience Utility class
  250.         SimpleUniverse u = new SimpleUniverse(c);
  251.  
  252.         // This will move the ViewPlatform back a bit so the
  253.         // objects in the scene can be viewed.
  254.         u.getViewingPlatform().setNominalViewingTransform();
  255.         u.addBranchGraph(scene);
  256.  
  257.  
  258.     // Create GUI
  259.     JPanel p = new JPanel();
  260.     BoxLayout boxlayout = new BoxLayout(p, 
  261.                         BoxLayout.Y_AXIS);
  262.     p.add(createGeometryByReferencePanel());
  263.     p.setLayout(boxlayout);
  264.  
  265.     contentPane.add("South", p);
  266.     
  267.  
  268.  
  269.     }
  270.     public void actionPerformed(ActionEvent e) {
  271.     Object target = e.getSource();
  272.     if (target == geomType) {
  273.         shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
  274.  
  275.     } 
  276.     else if (target == transparency) {
  277.         if (transparency.isSelected()) {
  278.         transp.setTransparencyMode(TransparencyAttributes.BLENDED);
  279.         }
  280.         else {
  281.         transp.setTransparencyMode(TransparencyAttributes.NONE);
  282.         }
  283.  
  284.  
  285.     } 
  286.  
  287.  
  288.     }
  289.  
  290.  
  291.                
  292.     public static void main(String[] args) {
  293.     Frame frame = new MainFrame(new InterleavedTest(), 800, 800);
  294.     }
  295.                
  296.     public GeometryArray createGeometry (int type) {
  297.     GeometryArray tetra = null;
  298.     if (type == 1) {
  299.         tetra =new TriangleArray(12, 
  300.                      TriangleArray.COORDINATES|
  301.                      TriangleArray.COLOR_3|
  302.                      /*
  303.                        TriangleArray.NORMAL_3|
  304.                        TriangleArray.TEXTURE_2 |
  305.                        */
  306.                      TriangleArray.INTERLEAVED|
  307.                      TriangleArray.BY_REFERENCE);
  308.         
  309.         tetra.setInterleavedVertices(interleaved);
  310.  
  311.     }
  312.     else if (type == 2) {
  313.         tetra = new TriangleStripArray(12,
  314.                        TriangleStripArray.COORDINATES|
  315.                        TriangleStripArray.COLOR_3|
  316.                        /*
  317.                          TriangleArray.NORMAL_3|
  318.                          TriangleArray.TEXTURE_2 |
  319.                          */
  320.                        TriangleStripArray.INTERLEAVED|
  321.                        TriangleStripArray.BY_REFERENCE,
  322.                        stripVertexCounts);
  323.         tetra.setInterleavedVertices(interleaved);
  324.  
  325.         
  326.     }
  327.     else if (type == 3) { // Indexed Geometry
  328.         tetra = new IndexedTriangleArray(4,
  329.                          IndexedTriangleArray.COORDINATES|
  330.                          IndexedTriangleArray.COLOR_3|
  331.                          /*
  332.                            IndexedTriangleArray.NORMAL_3|
  333.                            IndexedTriangleArray.TEXTURE_2 |
  334.                            */
  335.                          IndexedTriangleArray.INTERLEAVED|
  336.                          IndexedTriangleArray.BY_REFERENCE,
  337.                          12);
  338.         tetra.setInterleavedVertices(indexedInterleaved);
  339.         ((IndexedTriangleArray)tetra).setCoordinateIndices(0, indices);
  340.         ((IndexedTriangleArray)tetra).setColorIndices(0, indices);
  341.     }
  342.     else if (type == 4) { // Indexed strip geometry
  343.         tetra = new IndexedTriangleStripArray(4,
  344.                           IndexedTriangleStripArray.COORDINATES|
  345.                           IndexedTriangleStripArray.COLOR_3|
  346.                           /*
  347.                             IndexedTriangleArray.NORMAL_3|
  348.                             IndexedTriangleArray.TEXTURE_2 |
  349.                             */
  350.                           IndexedTriangleStripArray.INTERLEAVED|
  351.                           IndexedTriangleStripArray.BY_REFERENCE,
  352.                           12,
  353.                           stripVertexCounts);
  354.         tetra.setInterleavedVertices(indexedInterleaved);
  355.         ((IndexedTriangleStripArray)tetra).setCoordinateIndices(0, indices);
  356.         ((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
  357.     }
  358.     else if (type == 5) { // Interleaved array
  359.     }
  360.     return tetra;
  361.     }
  362. }
  363.